package com.xiaoma.myaudience.biz.util;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;

import com.xiaoma.myaudience.R;
import com.xiaoma.myaudience.util.SystemUtils;
import com.xiaoma.myaudience.util.UtilConstants;

/**
 * <br/>字符串工具类
 * @author wjying
 *
 */
public class VideoStringUtils {    
    /**
     * base64 编码  指定字符集charset
     * @param str
     * @param charset
     * @return
     */
    public static String base64Encode(String str, String charset) {
        try {
            if(null == str)
                return "";
            return it.sauronsoftware.base64.Base64.encode(str, charset);
        } catch (RuntimeException e) {
            return str;
        }
    }

    /**
     * base64 解码 charset， 指定字符集charset
     * @param str
     * @param charset
     * @return
     */
    public static String base64Decode(String str, String charset) {
        try {
            if(null == str)
                return "";
            
            return it.sauronsoftware.base64.Base64.decode(str, charset);
        } catch (RuntimeException e) {
            return str;
        }
    }

    /**
     * URL编码
     * 
     * @param str
     * @return
     */
    public static String URLEncode(String str) {
        try {
            if(null == str)
                return "";
            
            return URLEncoder.encode(str, UtilConstants.UTF_8);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * URL解码
     * 
     * @param str
     * @return
     */
    public static String URLDecode(String str) {
        try {
            if(null == str)
                return "";
            return URLDecoder.decode(str, UtilConstants.UTF_8);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 通用编码
     * 
     * @param str
     * @return
     */
    public static String encode(String str) {
        try {
            str = base64Encode(str, UtilConstants.UTF_8);
            return URLEncode(str);
        } catch (Exception e) {
            return str;
        }
    }

    /**
     * 通用解码
     * 
     * @param str
     * @return
     */
    public static String decode(String str) {
        try {
            str = URLDecode(str);
            return base64Decode(str, UtilConstants.UTF_8);
        } catch (Exception e) {
            return str;
        }
    }
    
    /**
     * TODO ...感觉这个可以再改改
     * @param src
     * @return
     */
    public static String getUserName(String src) {
        System.out.println(src);
		if (src == null || src.equals("")) {
			return "";
		}
		StringBuilder s = new StringBuilder(src);
		if(src.startsWith("网易")) {
			s = s.delete(0, 2);
		}
		
		if(src.contains("原贴")) {
			src = s.substring(0, s.indexOf("网友")+2) + " 的原贴：";
		} else {
			src = s.substring(0, s.indexOf("网友")+2) + "：";
		} 
			
		return src;
	}

    /**
	 * 替换HTML字符.
	 */
	public static String htmlDecoder(String src) throws Exception {
		if (src == null || src.equals("")) {
			return "";
		}

		String dst = src;
		dst = replaceAll(dst, "&lt;", "<");
		dst = replaceAll(dst, "&rt;", ">");
		dst = replaceAll(dst, "&quot;", "\"");
		dst = replaceAll(dst, "&039;", "'");
		dst = replaceAll(dst, "&nbsp;", " ");
		dst = replaceAll(dst, "&nbsp", " ");
		dst = replaceAll(dst, "<br>", "\n");
		dst = replaceAll(dst, "\r\n", "\n");
		dst = replaceAll(dst, "&#8826;", "•");
		dst = replaceAll(dst, "&#8226;", "•");
		dst = replaceAll(dst, "&#9642;", "•");
		return dst;
	}
	
	public static String replaceAll(String src, String fnd, String rep)
			throws Exception {
		
		if (src == null || src.equals("")) {
			return "";
		}
		
		String dst = src;
		
		int idx = dst.indexOf(fnd);
		
		while (idx >= 0) {
			dst = dst.substring(0, idx) + rep
					+ dst.substring(idx + fnd.length(), dst.length());
			idx = dst.indexOf(fnd, idx + rep.length());
		}
		
		return dst;
	}

	/**
	 * 获取时间差
	 * @param date
	 * @return
	 */
	public static String getDateDiff(Context context, String date) {
		if(null==date || "".equals(date)){
			return "";
		}
		String fomat = "yyyy-MM-dd HH:mm:ss";
		try {
			SimpleDateFormat dfs = new SimpleDateFormat(fomat);  
			Date begin=dfs.parse(date);  
			Date end = dfs.parse(SystemUtils.getCurrentTime(fomat));
			long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒  
			
			long day=between/(24*3600);  
			long hour=between%(24*3600)/3600;  
			long minute=between%3600/60;  
			long second=between%60/60;
			
			if(day>=1){
				date =  new SimpleDateFormat("yyyy-MM-dd").format(begin);
			}else if(hour>0 && day == 0){
			    date = context.getString(R.string.date_diff_hour, hour);
//				date = hour + "小时前";
			}else if(minute>0 && hour==0 && day == 0){
			    date = context.getString(R.string.date_diff_min, minute);
//				date = minute + "分钟前";
			}/*else if(second>0 && minute==0&&hour==0 && day == 0){
				date = second + "秒前";
			}*/else {
			    date = context.getString(R.string.date_diff_sec);
//				date = "1分钟前";
			}
			
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return date;
	}
	
	public static float getScore(double d) {
		return (float) (d*5/Constant.TOTAL_SCORE_CONSTANT);
	}
}
